import { findByDictCode, getPageList } from '../../api/index';
export const _toggleLoading = (payload) => {
  return {
    type: 'TOGGLE_LOADING',
    payload,
  };
};

export const _toggleListLoading = (payload) => {
  return {
    type: 'TOGGLE_LIST_LOADING',
    payload,
  };
};

export const _getHomeCategoriesInfoData = (payload) => {
  return {
    type: 'GET_HOME_CATEGORIES_INFO',
    payload,
  };
};

export const _getHomeListDataInfoData = (payload) => {
  return {
    type: 'GET_HOME_LISTDATA_INFO',
    payload,
  };
};

export function getHomeCategoriesInfo() {
  return async (dispatch) => {
    try {
      await dispatch(_toggleLoading(true));
      const hostypeListResult = await findByDictCode('Hostype');
      const districtListResult = await findByDictCode('Beijin');
      await hostypeListResult.unshift({ name: '全部', value: '', id: 'all' });
      await districtListResult.unshift({ name: '全部', value: '', id: 'all' });
      const categoriesObj = {
        hostypeList: hostypeListResult,
        districtList: districtListResult,
      };
      await dispatch(_getHomeCategoriesInfoData(categoriesObj));
    } catch (error) {
      await dispatch(_toggleLoading(false));
      console.log(error);
    }
  };
}

export function getHomeListDataInfo({ page, limit, searchObj }) {
  return async (dispatch) => {
    try {
      await dispatch(_toggleListLoading(true));
      const listResult = await getPageList(page, limit, searchObj);
      const listDataObj = {
        list: listResult.content,
        pages: listResult.totalPages,
        total: listResult.totalElements,
      };
      await dispatch(_getHomeListDataInfoData(listDataObj));
      await dispatch(_toggleLoading(false));
      await dispatch(_toggleListLoading(false));
    } catch (error) {
      await dispatch(_toggleLoading(false));
      await dispatch(_toggleListLoading(false));
      console.log(error);
    }
  };
}
